#include <stdio.h>
#include <stdlib.h>

//缺书单
//void less()

struct booklist{
	char Class[20];   //班级 
	int ID;           //学号 
	char name[20];    //姓名 
	char ISBN[20];    //书号 
	char press[50];   //出版社 
	double price;     //单价 
	char add[100];    //地址 	
	char title[30];   //书名 
	int number;       //数量 
	char phone[30];   //电话 
}; 

int main()
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("lessbook.txt","r");
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%d",&book[i].number);
		fscanf(fp,"%s",&book[i].press);
		fscanf(fp,"%s",&book[i].add);
		fscanf(fp,"%s",&book[i].phone);
		i++;
	}
	fclose(fp);
	printf("\t*********************************************************************************\t\n");
	printf("\t*\t\t\t\t\t缺书单\t\t\t\t\t*\t\n");
	printf("\t*********************************************************************************\t\n");
	printf("\t*\t书号\t\t\t数量\t出版社\t\t地址\t电话\t\t*\t\n");
	int n = i;
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t%d",book[i].number);
		printf("\t%s",book[i].press);
		printf("\t%s",book[i].add);
		printf("\t%s\t*\t\n",book[i].phone);
		printf("\t*********************************************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.删除\t\t");
	printf("\n\t\t3.退出\t\t");
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)
	{
		system("cls");
		printf("\n\t书号：");
		scanf("%s",&book[i].ISBN);
		getchar();
		printf("\t数量：");
		scanf("%d",&book[i].number);
		printf("\t出版社：");
		scanf("%s",&book[i].press);
		getchar(); 
		printf("\t电话：");
		scanf("%s",&book[i].phone);
		getchar();
		fp = fopen("lessbook.txt","a");
		if(fp == NULL)
	    {
		    printf("未成功打开文件");
		    return;
	    }
		fprintf(fp,"\n%s %d %s %s %s",book[i].ISBN,book[i].number,book[i].press,book[i].phone);
		fclose(fp);
		printf("\n\t信息录入成功!");
		system("pause");
		again();
	}
	if(x == 2)
	{
		system("cls");
		int flag = 0;
		char isbn[20];
		printf("\n\t书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,book[i].ISBN) == 0)
			{
				flag = 1;
				for(int j=i;j<n-1;j++)
				{
					book[j] = book[j+1];
				}
				if(flag == 1)
				{
					break;
				}
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号");
			system("pause");
			again();
		}
		if(flag == 1)
		{
			fp = fopen("lessbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return;	 
	        }
			for(i=0;i<n-1;i++)
			{
				fprintf(fp,"\n%s ",book[i].ISBN);
		        fprintf(fp,"%d ",book[i].number);
		        fprintf(fp,"%s ",book[i].press);
		        fprintf(fp,"%s ",book[i].add);
		        fprintf(fp,"%s",book[i].phone);
			}
			fclose(fp);
			printf("\n\t删除成功"); 
			system("pause");
			again(); 
		}
	}
	if(x == 3)
	{
		again();
	}
	return 0;
}
